import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-12-06
 * Time: 20:47
 */

class Solution5_5 {
    public int minNumberOfFrogs(String croakOfFrogs) {
        char[] str = croakOfFrogs.toCharArray();
        String t = "croak";
        int n = t.length();
        int[] hash = new int[n];

        Map<Character,Integer> hashMap = new HashMap<>();//<字符，字符对应下标>

        for(int i = 0 ; i < n ; i++){
            hashMap.put(t.charAt(i),i);
        }

        for(char ch : str){
            if(ch == t.charAt(0)){
                if(hash[n-1] != 0){
                    hash[n-1]--;
                    hash[0]++;
                }else{
                    hash[0]++;
                }
            }else{
                int i = hashMap.get(ch);
                if(hash[i-1] == 0){
                    return -1;
                }else{
                    hash[i-1]--;
                    hash[i]++;
                }
            }
        }

        for(int i = 0 ; i < n-1 ; i++){
            if(hash[i] != 0){
                return -1;
            }
        }
        return hash[n-1];
    }
}




class Solution5 {
    public int minNumberOfFrogs(String croakOfFrogs) {

        //创建hash表，创建字符数组，遍历数组
        Map<Character,Integer> hash = new HashMap<>();
        char[] str = croakOfFrogs.toCharArray();
        hash.put('c',0);hash.put('r',0);hash.put('o',0);hash.put('a',0);hash.put('k',0);
        for(int i = 0 ; i < str.length ; i++){
            if(str[i] == 'c'){
                if(hash.get('k') == 0){
                    hash.put('c',hash.get('c')+1);
                }else{
                    hash.put('c',hash.get('c')+1);
                    hash.put('k',hash.get('k')-1);
                }
            }else{
                if(str[i] == 'r'){
                    if(hash.get('c') != 0){
                        hash.put('c',hash.get('c')-1);
                        hash.put('r',hash.get('r')+1);
                    }else{
                        return -1;
                    }
                }else if(str[i] == 'o'){
                    if(hash.get('r') != 0){
                        hash.put('r',hash.get('r')-1);
                        hash.put('o',hash.get('o')+1);
                    }else{
                        return -1;
                    }

                }else if(str[i] == 'a'){
                    if(hash.get('o') != 0){
                        hash.put('o',hash.get('o')-1);
                        hash.put('a',hash.get('a')+1);
                    }else{
                        return -1;
                    }

                }else if(str[i] == 'k'){
                    if(hash.get('a') != 0){
                        hash.put('a',hash.get('a')-1);
                        hash.put('k',hash.get('k')+1);
                    }else{
                        return -1;
                    }
                }else{}
            }
        }

        if(hash.get('c') != 0 || hash.get('r') != 0 ||
                hash.get('o') != 0 || hash.get('a') != 0 ){
            return -1;
        }
        int ret = hash.get('k');
        return ret;
        //判断
    }
}






















public class Test5 {
}
